
#include "i2c.h"


#define SDA_DIR (PM1.1 = 0) 
#define SCL_DIR (PM1.0 = 0)
#define S_SDA	(P1.1 = 1)	
#define S_SCL 	(P1.0 = 1)	
#define C_SDA	(P1.1 = 0)	
#define C_SCL   (P1.0 = 0)	
 
#define SDA_IN   (PM1.1 = 1)    
#define SDA_OUT  (PM1.1 = 0)	
 
#define READ_SDA  (P1.1)


void Init_I2c() { 
	SDA_DIR; 
	SCL_DIR; 
	C_SCL; 
	Stop(); 
}  


void delay() { 
	unsigned int i; 
	for(i=0;i<1;i++) //1的话不知道可不可以
	{ 
		NOP(); 
	} 
}




void Start() { 
	SDA_OUT; 
	S_SDA; 
   	S_SCL;            //scl high 
	delay(); 	 
	C_SDA; 
	delay(); 
	C_SCL;
} 
 

 
void Stop() { 
	SDA_OUT;	
	C_SCL;             
	C_SDA;  	
	delay(); 
	SDA_OUT;              
	S_SCL;               
	S_SDA; 
	delay(); 			 
} 
 

 
void Ack(){ 
	SDA_OUT;
	C_SDA;
	delay();
	S_SCL;
	delay();
	C_SCL;
	delay();
	S_SDA;
} 



void NoAck() 
{ 
	SDA_OUT;
	S_SDA;
	delay();
	S_SCL;
	delay();
	C_SCL;
	delay();
} 
 

 
	
uint8_t TestAck(){ 
	uint8_t ack=1; 
	SDA_IN;
	S_SDA; 
	S_SCL; 
	delay(); 
	
	delay(); 
	C_SCL; 
	delay(); 
	return(ack); 
} 


uint8_t Read8Bit() { 
	uint8_t temp,rbyte=0; 
	SDA_IN; 
	for(temp=0;temp<8;temp++) 
	{ 
		C_SCL; 
		delay(); 
		S_SCL;
		rbyte=rbyte<<1; 
		if(READ_SDA) 
			rbyte=rbyte|0x1; 
		delay(); 
		C_SCL; 
		delay(); 
	} 
	return(rbyte); 
} 

void Write8Bit(uint8_t input){ 
	uint8_t temp,q0=0; 
	SDA_OUT; 
	C_SCL; 
	for(temp=0;temp<8;temp++) 
	{ 			
		q0=input&0x80;		
		if(q0==0x80) 
			S_SDA; 
		else 
			C_SDA; 
      		delay(); 
		S_SCL; 
		delay(); 
		C_SCL; 
		delay(); 
		input=input<<1;  
	} 
}  

void I2cWrite(uint8_t addr,uint8_t reg, uint8_t data){
    Start() ;
    Write8Bit(addr);
    TestAck();
    Write8Bit(reg);
    TestAck();
    Write8Bit(data);
    TestAck();
    Stop();
	//可能要加延时函数
}
void I2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf){
    Start();
    Write8Bit(addr);
    TestAck();	
    Write8Bit(reg);
    TestAck();	
    Start();
    Write8Bit(addr+1);
    TestAck();	
    while (len) {
        *buf = Read8Bit();
        if (len == 1)
            NoAck();
        else
            Ack();	
        buf++;
        len--;
    }
    Stop();
	//可能要加延时函数
}




//uint8_t i2c_start(void){
//	uint8_t wait=0;
//	IICAMK0 = 1U;		/* disable INTIICA0 interrupt */
//	IICE0 = 1;		/*Enable IIC operation.*/
//	WREL0 = 1;
//	LREL0 = 1;		/*Exit from other communications*/
//	IICAIF0=0;		/*Clear the IIC interrupt flag*/
//	STT0= 1 ;		/*sent start commond*/
//	while(!TRC0)	{
//		wait++;
//		if(wait>100)
//		return ERROR;
//	}
//	return OK;
//}
//
//uint8_t i2c_rstart(void){
//	uint8_t wait=0;
//	STT0= 1 ;		/*sent start commond*/
//	while(!TRC0)	{
//		wait++;
//		if(wait>100)return ERROR;
//	}
//	return OK;
//}
//
//uint8_t i2c_stop(void){
//	uint16_t wait=0;
//	SPT0=1;
//	while(!SPD0)	{
//		wait++;
//		if(wait>1000)return ERROR;
//	}
//	return OK;
//}
//
//uint8_t i2c_senddat(uint8_t dat){
//	if(!TRC0)return ERROR;	/*if isn't the send mode retrurn error*/
//	IICA0=dat;				/*send data*/
//	while(!IICAIF0);			/*send end*/
//	IICAIF0=0;
//	if(IICA0!=dat)return ERROR;
//	if(!ACKD0)return ERROR;
//	return OK;
//}
//
//uint8_t i2c_recedat(uint16_t mode)
//{
//	if(TRC0)return ERROR;
//	if(mode!=1){
//		ACKE0=1;
//	}
//	else{
//		ACKE0=0;
//	}
//	WREL0=1;
//	while(!IICAIF0);
//	IICAIF0=0;
//	return IICA0;
//}
